package com.yunqing.restkeeper.enterprise.web.controller.shop;

import com.yunqing.restkeeper.common.constants.SystemCode;
import com.yunqing.restkeeper.common.utils.Result;
import com.yunqing.restkeeper.enterprise.web.vo.shop.AddStoreVO;
import com.yunqing.restkeeper.shop.api.dto.StoreDTO;
import com.yunqing.restkeeper.shop.api.entity.Store;
import com.yunqing.restkeeper.shop.api.service.IStoreService;
import com.yunqing.restkeeper.web.common.response.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName StoreController
 * @Description 门店信息账号 前端控制器
 * @author: puyunqing
 * @Date: 2025/7/3
 */
@Slf4j
@Tag(name = "门店信息")
@RestController
@RequestMapping("/store")
public class StoreController {

    @DubboReference(version = "1.0.0", check = false)
    private IStoreService storeService;


    /**
     * 分页数据
     */
    @Operation(summary = "分页查询所有门店")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页码", required = true, schema = @Schema(type = "Integer")),
            @Parameter(in = ParameterIn.PATH, name = "pageSize", description = "分大小", required = true, schema = @Schema(type = "Integer")),
            @Parameter(in = ParameterIn.QUERY, name = "name", description = "门店名称", required = false, schema = @Schema(type = "String"))})
    @GetMapping(value = "/pageList/{page}/{pageSize}")
    public PageVO<Store> findListByPage(@PathVariable int page,
                                        @PathVariable int pageSize,
                                        @RequestParam(value = "name", required = false) String name) {
        return new PageVO<Store>(storeService.queryPageByName(page, pageSize, name));
    }

    /**
     * 新增门店
     */
    @Operation(summary = "新增数据")
    @PostMapping(value = "/add")
    public boolean add(@RequestBody AddStoreVO storeVO) {
        Store store = new Store();
        BeanUtils.copyProperties(storeVO, store);
        return storeService.save(store);
    }

    /**
     * 门店停用
     */
    @Operation(summary = "门店停用")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "主键", required = true, schema = @Schema(type = "String"))
    @PutMapping(value = "/disabled/{id}")
    public boolean disabled(@PathVariable String id) {

        Store store = storeService.getById(id);
        store.setStatus(SystemCode.FORBIDDEN);

        return storeService.updateById(store);
    }

    @Operation(summary = "获取门店省份信息")
    @GetMapping("/listProvince")
    @ResponseBody
    public List<String> listProvince() {
        return storeService.getAllProvince();
    }

    @Operation(summary = "根据省份获取门店列表")
    @GetMapping("/getStoreByProvince/{province}")
    @ResponseBody
    public List<StoreDTO> getStoreByProvince(@PathVariable String province) {
        return storeService.getStoreByProvince(province);
    }

    @Operation(summary = "获取当前商户管理的门店信息")
    @GetMapping(value = "/listManagerStores")
    public List<StoreDTO> getStoreListByManagerId(){
        return storeService.getStoreListByManagerId();
    }

    @Operation(summary = "门店切换")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "storeId", description = "门店Id", schema = @Schema(type = "String"))})
    @GetMapping(value = "/switchStore/{storeId}")
    public Result switchStore(@PathVariable("storeId") String storeId){
        return storeService.switchStore(storeId);
    }

}
